/*
 * @Descripttion:
 * @version:
 * @Author: -ViJia
 * @Date: 2020-05-22 14:43:27
 * @LastEditors: -ViJia
 * @LastEditTime: 2021-12-22 15:21:32
 */
import Vue from 'vue';
import env from '@/config/env';
import { ACCESS_TOKEN, USER_DATA } from '@/store/mutation-types';
import { welcome } from '@/utils/util';

// import { userMessage } from '@/api/passport';
// import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';
// import Server from '@/config/server';
// initial state
const state = {
  token: '',
  name: '',
  welcome: '',
  avatar: '',
  roles: [],
  info: {},
};

const actions = {
  // 登录
  Login({ commit }, { accessToken, UserData, rule }) {
    Vue.ls.set(ACCESS_TOKEN, accessToken, env.tokenExpires);
    Vue.ls.set(USER_DATA, UserData, env.tokenExpires);
    // Vue.ls.set(USER_RULE, rule, env.tokenExpires);
    commit('SET_TOKEN', accessToken);
    // commit('SET_NAME', name);
    const Name = UserData.name
    commit('SET_NAME', { Name, welcome: welcome() });
    const imgUrL = UserData.headimg
    commit('SET_AVATAR', imgUrL);
  },

  UserInfo({ commit }) {
    userMessage()
      .then(res => {
        if (res.data.code == 1) {
          // console.log(res.data);
          let data = res.data.data
          let infodata = {
            RouterList: data.data,
            RouteArr: data.route
          }
          Vue.ls.set(USER_RULE, infodata, env.tokenExpires);
        } else {
          showError(res.data.msg);
        }
      });
  },
};

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME: (state, { name, welcome }) => {
    state.name = name;
    // console.log(name);
    state.welcome = welcome;
  },

  SET_AVATAR: (state, avatar) => {
    // state.avatar = avatar;
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  SET_INFO: (state, info) => {
    state.info = info;
  },
};

export default {
  namespaced: true,
  state,
  actions,
  mutations,
};
